<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta property="og:type" content="website">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="http://example.com/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main">
  
    <article id="post-wxpython入门（3）菜单和工具栏" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/10/16/wxpython%E5%85%A5%E9%97%A8%EF%BC%883%EF%BC%89%E8%8F%9C%E5%8D%95%E5%92%8C%E5%B7%A5%E5%85%B7%E6%A0%8F/" class="article-date">
  <time datetime="2021-10-16T02:46:44.000Z" itemprop="datePublished">2021-10-16</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2021/10/16/wxpython%E5%85%A5%E9%97%A8%EF%BC%883%EF%BC%89%E8%8F%9C%E5%8D%95%E5%92%8C%E5%B7%A5%E5%85%B7%E6%A0%8F/">wxpython入门（3）菜单和工具栏</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        
      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/10/16/wxpython%E5%85%A5%E9%97%A8%EF%BC%883%EF%BC%89%E8%8F%9C%E5%8D%95%E5%92%8C%E5%B7%A5%E5%85%B7%E6%A0%8F/" data-id="ckut7892s0002w4uz5t530q55" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-wxpython入门（2）最简单的例子" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/10/16/wxpython%E5%85%A5%E9%97%A8%EF%BC%882%EF%BC%89%E6%9C%80%E7%AE%80%E5%8D%95%E7%9A%84%E4%BE%8B%E5%AD%90/" class="article-date">
  <time datetime="2021-10-16T02:13:20.000Z" itemprop="datePublished">2021-10-16</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2021/10/16/wxpython%E5%85%A5%E9%97%A8%EF%BC%882%EF%BC%89%E6%9C%80%E7%AE%80%E5%8D%95%E7%9A%84%E4%BE%8B%E5%AD%90/">wxpython入门（2）最简单的例子</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>下面我们来看一个最简单的wxpython例子，从中获得对wxpython的直观认识。</p>
<h1 id="开发环境介绍"><a href="#开发环境介绍" class="headerlink" title="开发环境介绍"></a>开发环境介绍</h1><p>下面列出的是我编写这些示例代码的环境：</p>
<ul>
<li>操作系统：Windows10</li>
<li>Python版本：3.8</li>
<li>Python IDE工具：Pycharm教育版（免费，好用）</li>
<li>wxpython（4.x版本，pip install wxpython默认得到的版本）</li>
</ul>
<p>打开pycharm，新建project，名字为wxpython_tutorial_code。</p>
<h1 id="最简单例子"><a href="#最简单例子" class="headerlink" title="最简单例子"></a>最简单例子</h1><p>新建Python文件simple.py。</p>
<p>在02-1-simple.py里写入下面的内容：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> wx</span><br><span class="line">app = wx.App()</span><br><span class="line">frame = wx.Frame(<span class="literal">None</span>, title=<span class="string">&#x27;最简单的例子&#x27;</span>)</span><br><span class="line">frame.Show()</span><br><span class="line">app.MainLoop()</span><br></pre></td></tr></table></figure>

<p>然后在pycharm里直接运行这个文件。</p>
<p>可以看到出现如下的窗口。这样我们的最简单的wxpython程序就跑起来了。</p>
<p>只需要短短的5行代码。可以快速看到效果，是可以激发人的学习热情的。</p>
<p><img src="https://gitee.com/teddyxiong53/playopenwrt_pic/raw/master/image-20211016102123511.png" alt="image-20211016102123511"></p>
<p>下面我们简单分析一下这5行代码。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> wx</span><br></pre></td></tr></table></figure>

<p>这个表示引入wx的包。每个wxpython程序都需要加这一行。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">app = wx.App()</span><br><span class="line">app.MainLoop()</span><br></pre></td></tr></table></figure>

<p>这2行分别是创建和运行app。app的作用是运行一个消息循环（就是一个阻塞的死循环），用来响应用户的各种操作。</p>
<p>app表示用户看不到的部分。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">frame = wx.Frame(<span class="literal">None</span>, title=<span class="string">&#x27;最简单的例子&#x27;</span>)</span><br><span class="line">frame.Show()</span><br></pre></td></tr></table></figure>

<p>这个表示创建一个窗口，第一个参数是表示父控件，这里为None是因为它自己就是顶层窗口，没有自己的父控件了。</p>
<p>frame.Show()这句是需要的，不然就看不到窗口了。</p>
<p>从这里，我们还可以看到wxpython类、函数命名的一个特点：全部都是首字母大写的驼峰标识，这个跟Python一般的PEP8标准是不符的，需要注意。</p>
<p><img src="https://gitee.com/teddyxiong53/playopenwrt_pic/raw/master/image-20211016103236524.png" alt="image-20211016103236524"></p>
<h1 id="wx-Frame"><a href="#wx-Frame" class="headerlink" title="wx.Frame"></a>wx.Frame</h1><p>wx.Frame是wxpython里最重要的组件之一。</p>
<p>它是一个容器。可以包含其他的组件。</p>
<p>wx.Frame组成：</p>
<ul>
<li>标题栏。可选。</li>
<li>边框。可选。</li>
<li>中央容器区域。</li>
</ul>
<p>wx.Frame构造函数，有7个参数：</p>
<ul>
<li>parent。必须写，没有则写None。</li>
<li>id。不指定则自动分配。一般自动分配就好了。</li>
<li>title。标题。</li>
<li>pos。窗口显示的位置（左上角的位置）</li>
<li>size。大小，是一个tuple，类似这样：(100,200)</li>
<li>style。一些标志位。默认是wx.DEFAULT_FRAME_STYLE.</li>
<li>name。名字。</li>
</ul>
<p>只有parent是没有默认值的。其余的都可以不指定。</p>
<p>wx.DEFAULT_FRAME_STYLE实际是这些标志的组合：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX | wx.RESIZE_BORDER wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN</span><br></pre></td></tr></table></figure>

<h1 id="改变窗口的大小"><a href="#改变窗口的大小" class="headerlink" title="改变窗口的大小"></a>改变窗口的大小</h1><p>可以创建的时候设置，也可以在运行中通过SetSize函数来设置。</p>
<p>创建时指定大小：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> wx</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Example</span>(<span class="params">wx.Frame</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, parent, title</span>):</span></span><br><span class="line">        <span class="built_in">super</span>(Example, self).__init__(parent, title=title, size=(<span class="number">350</span>,<span class="number">250</span>))</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span>():</span></span><br><span class="line">    app = wx.App()</span><br><span class="line">    frame = Example(<span class="literal">None</span>, title=<span class="string">&#x27;指定窗口大小&#x27;</span>)</span><br><span class="line">    frame.Show()</span><br><span class="line">    app.MainLoop()</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    main()</span><br></pre></td></tr></table></figure>

<h1 id="指定仓库的位置"><a href="#指定仓库的位置" class="headerlink" title="指定仓库的位置"></a>指定仓库的位置</h1><p>一般让窗口居中就好了。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> wx</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Example</span>(<span class="params">wx.Frame</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, parent, title</span>):</span></span><br><span class="line">        <span class="built_in">super</span>(Example, self).__init__(parent, title=title, size=(<span class="number">350</span>,<span class="number">250</span>))</span><br><span class="line">        self.Center() <span class="comment"># 加这行就居中了。</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span>():</span></span><br><span class="line">    app = wx.App()</span><br><span class="line">    frame = Example(<span class="literal">None</span>, title=<span class="string">&#x27;窗口居中&#x27;</span>)</span><br><span class="line">    frame.Show()</span><br><span class="line">    app.MainLoop()</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    main()</span><br></pre></td></tr></table></figure>


      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/10/16/wxpython%E5%85%A5%E9%97%A8%EF%BC%882%EF%BC%89%E6%9C%80%E7%AE%80%E5%8D%95%E7%9A%84%E4%BE%8B%E5%AD%90/" data-id="ckut7892q0001w4uz52qc2hxz" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-wxpython入门（1）介绍" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/10/16/wxpython%E5%85%A5%E9%97%A8%EF%BC%881%EF%BC%89%E4%BB%8B%E7%BB%8D/" class="article-date">
  <time datetime="2021-10-16T01:36:19.000Z" itemprop="datePublished">2021-10-16</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2021/10/16/wxpython%E5%85%A5%E9%97%A8%EF%BC%881%EF%BC%89%E4%BB%8B%E7%BB%8D/">wxpython入门（1）介绍</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="wxpython基本情况"><a href="#wxpython基本情况" class="headerlink" title="wxpython基本情况"></a>wxpython基本情况</h1><p>wxpython是一个跨平台的GUI框架。</p>
<p>使用wxpython创建的GUI程序，可以直接在windows、macos、类unix系统上直接跨平台运行。</p>
<p>wxpython是对wxwidgets图形库（使用C++编写）的Python包装库，使用Python进行界面开发，门槛更低，上手更容易，适合用来快速实现一些简单的图形界面程序。</p>
<h1 id="wxpython的模块组成"><a href="#wxpython的模块组成" class="headerlink" title="wxpython的模块组成"></a>wxpython的模块组成</h1><p>主要模块划分如下图所示：</p>
<p><img src="https://gitee.com/teddyxiong53/playopenwrt_pic/raw/master/image-20211016100159542.png" alt="image-20211016100159542"></p>
<h1 id="widget的分组"><a href="#widget的分组" class="headerlink" title="widget的分组"></a>widget的分组</h1><p><img src="https://gitee.com/teddyxiong53/playopenwrt_pic/raw/master/image-20211016101044208.png" alt="image-20211016101044208"></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/10/16/wxpython%E5%85%A5%E9%97%A8%EF%BC%881%EF%BC%89%E4%BB%8B%E7%BB%8D/" data-id="ckut7892k0000w4uz2bb4bze0" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/10/">October 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/10/16/wxpython%E5%85%A5%E9%97%A8%EF%BC%883%EF%BC%89%E8%8F%9C%E5%8D%95%E5%92%8C%E5%B7%A5%E5%85%B7%E6%A0%8F/">wxpython入门（3）菜单和工具栏</a>
          </li>
        
          <li>
            <a href="/2021/10/16/wxpython%E5%85%A5%E9%97%A8%EF%BC%882%EF%BC%89%E6%9C%80%E7%AE%80%E5%8D%95%E7%9A%84%E4%BE%8B%E5%AD%90/">wxpython入门（2）最简单的例子</a>
          </li>
        
          <li>
            <a href="/2021/10/16/wxpython%E5%85%A5%E9%97%A8%EF%BC%881%EF%BC%89%E4%BB%8B%E7%BB%8D/">wxpython入门（1）介绍</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2021 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>